package hrms.controller;

import hrms.entity.Absenteeism;
import hrms.entity.Result;
import hrms.service.AbsenteeismService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/absenteeism")
public class AbsenteeismController {
    @Autowired
    private AbsenteeismService absenteeismService;

    @PostMapping("/getall")
    public Result showAllAbsenteeism(){
        Map<String,Object> modelMap = new HashMap<String,Object>();
        modelMap.put("list",absenteeismService.selectAllAbsenteeism());
        return Result.success().addMap(modelMap);
    }

    @PostMapping("/add/{staff_id}")
    public Result addNewAbsenteeism(@PathVariable("staff_id")Integer staff_id, Absenteeism absenteeism){
        Map<String,Object> map = new HashMap<String,Object>();
        int res = absenteeismService.insertAbsenteeism(absenteeism,staff_id);
        if(res<0){
            return Result.fail().error("插入失败");
        }
        map.put("success","插入成功");
        return Result.success().addMap(map);
    }

    @PostMapping("/delete/{id}")
    public  Result deleteAbsenteeism(@PathVariable("id")Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        int res = absenteeismService.deleteAbsenteeism(id);
        if(res<0){
            return Result.fail().error("删除失败");
        }
        return Result.success().addMap(map);
    }

    @PostMapping("/search/{year}/{month}")
    public Result searchByYearAndMonth(@PathVariable("year")Integer year,@PathVariable("month")int month){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Absenteeism> list = absenteeismService.selectAbsenteeismByYear(year,month);
        if (list.size()==0){
            return Result.fail().error("请输入正确的年份和日期");
        }
        map.put("list",list);
        return Result.success().addMap(map);
    }

    @PostMapping("/searchid/{id}")
    public Result searchById(@PathVariable("id")Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Absenteeism> list = absenteeismService.selectAbsenteeismByStaffId(id);
        if (list.size()==0){
            return Result.fail().error("请再次检查员工的id");
        }
        map.put("list",list);
        return Result.success().addMap(map);
    }
}
